<?php
include('includes/structure.php');

function fks_main() {
	switch(true){
		case isset($_POST['buscar']):
			do_buscar();
			break;
		case isset($_POST['generar']):
			do_generar();
			break;
		default:
			do_default();
			break;
	}
}

function do_default(){
	$_SESSION['init']=0;
?>
<form name="buscar" action="" method="post" onSubmit = "return JS_resetpwd(this)">   
	<table cellspacing="2" cellpadding="0">				
		<tr>
		    <td>N&uacute;mero de Documento: </td><td><input type="text" name="nro_doc" size="20" maxlength="20"></td>
		</tr>		
		<tr>    
		    <td>Nombre: </td><td><input type="text" name="nombre" size="15" maxlength="15"></td>
		</tr> 	
		<tr>    
		   <td>Apellido: </td><td><input type="text" name="apellido" size="15" maxlength="15"></td>
		</tr> 	
        <tr>    
		   <td>Raz&oacute;n Social: </td><td><input type="text" name="razon_social" size="15" maxlength="15"></td>
		</tr>
		<tr>
			<td><input type="submit" name="buscar" value="Buscar"></td>
		</tr>
	</table>
</form>
<?php
}

function do_buscar(){
?>
<form name="lista" action="" method="post">
	<input type="hidden" name="ident" value="" />
	<table cellspacing="0" cellpadding="5">
		<tr>  
		    <th>Nombre / Raz&oacute;n Social</th>    
	<!--	<th>Apellido</th>                     -->                
    		<th>Documento</th>
    <!--    <th>Raz&oacute;n Social</th>          -->
			<th></th>		  		  
		</tr>
<?php
	//$documento = empty($_POST["nro_doc"])?'null':$_POST["nro_doc"];
	//$nombre    = empty($_POST["nombre"])?'null':$_POST["nombre"];
	//$apellido  = empty($_POST["apellido"])?'null':$_POST["apellido"];

	$documento = $_POST["nro_doc"] ==''?'null':$_POST["nro_doc"];
	$nombre    = $_POST["nombre"]  ==''?'null':"'".$_POST["nombre"]."'";
	$apellido  = $_POST["apellido"]==''?'null':"'".$_POST["apellido"]."'";
	$razsoc    = $_POST["razon_social"]==''?'null':"'".$_POST["razon_social"]."'";
	
	$color = 1;	
   
	$sql= "call sp_s_filtrarclientes($apellido,$nombre,$razsoc,$documento,null)";
	
	$result = dbh_query($sql);

	while($row=dbh_fetch($result)){
	
	    if($color == 1){ 
            echo '<tr bgcolor="#BFBFBF">';
            $color = 2;
         } else {
            echo '<tr bgcolor="#D0D0D0">';
            $color = 1;
         }
?>
		<tr>            
			<td><?php
					if($row['tx_razonsocial'] == '') {
					  echo $row['tx_nombre'].' '.$row['tx_apellido'];
					} else {    
					  echo $row['tx_razonsocial']; 
					}     	
				?>
            </td>         
			<td><?php echo $row['nu_documento'] ?></td>
			<td><input type="submit" name="generar" value="Generar Contrase&ntilde;a" onClick="{document.lista.ident.value=<?php echo $row['cd_entidad'] ?>;}"></td>
		</tr>
<?php
	}
?>
	</table>
</form>
<?php
}

function do_generar(){
	$p0 = $_POST["ident"];
	$sql = "call sp_u_resetpwd($p0)"; 
	$result= dbh_query($sql);
	if($row=dbh_fetch($result)){
		$pwd= $row['tx_password'];
		$email= $row['tx_email_usuario'].'@'.$row['tx_email_dominio'];
		
		//funcion para enviar mail
?>
<div>
	<table>
		<tr>
			<td>Contrase&ntilde;a generada</td>
			<td>: <?php echo $pwd ?></td>
		</tr>
		<tr>
			<td>Enviada por correo a</td>
			<td>: <?php echo $email ?></td>
		</tr>
	</table>
</div>
<br /><br /><input type="button" name="boton" value="Continuar" onClick="window.location.href='';" />
<?php
	}
}
?>